package com.zzh.myapplication.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;
import net.icarplus.car.views.PasswordInputView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/14 12:06
 * 修改人：Gophy
 * 修改时间：16/3/14 12:06
 * 修改备注：
 */
public class OrderPayCenterActivity extends BaseActivity implements View.OnClickListener {

    private Button btnPayNow;

    private ImageView ivAliPay;
    private ImageView ivWechat;
    private ImageView ivUnionPay;
    private ImageView iv_order_pay;

    private RelativeLayout alipayRelayout;
    private RelativeLayout weChatRelayout;
    private RelativeLayout unionpayRelayout;

    private final int typeDefault = -1;
    private final int typeAlipay = 1;  //支付宝
    private final int typeWeChat = 2;  //微信
    private final int typeUnionPay = 3;//银联
    private int whatType = typeDefault;
    public TextView pay_center_serial_num_txt;
    public TextView pay_center_money_num_txt;
    public TextView ssfy;
    private AlertDialog.Builder builder;
    private PasswordInputView mView;
    private AlertDialog _cardialog;
    public String depositChargeItems;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_order_pay_center);
    }

    @Override
    public void initComponent() {
        btnPayNow = (Button) findViewById(R.id.btn_pay_now);
        ivAliPay = (ImageView) findViewById(R.id.recharge_alipay_imgv);
        iv_order_pay = (ImageView) findViewById(R.id.iv_order_pay);
        ivUnionPay = (ImageView) findViewById(R.id.recharge_unionpay_imgv);
        ivWechat = (ImageView) findViewById(R.id.recharge_wechat_imgv);
        pay_center_serial_num_txt = (TextView) findViewById(R.id.pay_center_serial_num_txt);
        pay_center_money_num_txt = (TextView) findViewById(R.id.pay_center_money_num_txt);
        ssfy = (TextView) findViewById(R.id.ssfy);
        alipayRelayout = (RelativeLayout) findViewById(R.id.recharge_alipay_relayout);
        weChatRelayout = (RelativeLayout) findViewById(R.id.recharge_wechat_relayout);
        unionpayRelayout = (RelativeLayout) findViewById(R.id.recharge_unionpay_relayout);

    }

    @Override
    public void setListener() {
        btnPayNow.setOnClickListener(this);
        alipayRelayout.setOnClickListener(this);
        weChatRelayout.setOnClickListener(this);
        unionpayRelayout.setOnClickListener(this);
        iv_order_pay.setOnClickListener(this);
    }

    @Override
    public void onAfterInit() {
        if (getIntent().getStringExtra("ordernum") != null) {
            pay_center_serial_num_txt.setText(getIntent().getStringExtra("ordernum"));
            pay_center_money_num_txt.setText(getIntent().getStringExtra("money"));
            ssfy.setText(getIntent().getStringExtra("money"));
            Log.e("money", "" + getIntent().getStringExtra("money"));
        } else {
            pay_center_serial_num_txt.setText(getIntent().getStringExtra("appno"));
            depositChargeItems = getIntent().getStringExtra("depositChargeItems");
            pay_center_money_num_txt.setText(depositChargeItems);
            ssfy.setText(depositChargeItems);
            Log.e("depositChargeItems", "" + depositChargeItems);
        }

    }

    TextWatcher mTextWatcher = new TextWatcher() {

        private CharSequence temp;
        private int editStart;
        private int editEnd;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            temp = s;
        }

        Intent intent = new Intent();

        @Override
        public void afterTextChanged(Editable s) {
            editStart = mView.getSelectionStart();
            editEnd = mView.getSelectionEnd();
            if (temp.toString().trim().equals("123456")) {
                _cardialog.dismiss();
                // 输密码
                Toast.makeText(OrderPayCenterActivity.this,
                        "123456", Toast.LENGTH_SHORT)
                        .show();
                intent.setClass(OrderPayCenterActivity.this, PaySucceedActivity.class);
                intent.putExtra("ordernum", getIntent().getStringExtra("ordernum"));
                start_Activity(intent);
            } else if (temp.length() == 6 && !temp.toString().trim().equals("123456")) {
                Toast.makeText(OrderPayCenterActivity.this,
                        "请输入正确的密码", Toast.LENGTH_SHORT)
                        .show();
                temp = null;
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recharge_alipay_relayout:
                whatType = typeAlipay;
                changeStatus(whatType);
                break;
            case R.id.recharge_wechat_relayout:
                whatType = typeWeChat;
                changeStatus(whatType);
                break;
            case R.id.recharge_unionpay_relayout:
                whatType = typeUnionPay;
                changeStatus(whatType);
                break;
            case R.id.btn_pay_now:
                Intent intent = new Intent();
                builder = new AlertDialog.Builder(this);
                View carview = View.inflate(this, R.layout.dialog_enterpassword, null);
                mView = (PasswordInputView) carview.findViewById(R.id.passwordInputView);
                builder.setView(carview);
                _cardialog = builder.show();
                mView.addTextChangedListener(mTextWatcher);
                if (whatType == typeAlipay) {
                    T.showLong(OrderPayCenterActivity.this, "你选择的是支付宝支付");
                } else if (whatType == typeWeChat) {
                    T.showLong(OrderPayCenterActivity.this, "你选择的是微信支付");
                } else if (whatType == typeUnionPay) {
                    T.showLong(OrderPayCenterActivity.this, "你选择的是银联支付");
                }

                break;
            case R.id.iv_order_pay:
                startActivity(new Intent(OrderPayCenterActivity.this, MainActivity.class));
                finish_Activity();
                break;

            default:
                break;
        }

    }

    public void GetPrepayHourOrderParams() {
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        JSONObject arrayobjeast = new JSONObject();
        try {
            object.put("mobile", SPUtils.getString(this, "LoginInfo", null));
            object.put("appNo", getIntent().getStringExtra("appno"));
            object.put("payPassword", "");
            object.put("isBusiPay", "");
            array.put(arrayobjeast);
            arrayobjeast.put("currencyType", "0201");
            arrayobjeast.put("points", "");
            arrayobjeast.put("payAmount", getIntent().getStringExtra("depositChargeItems"));
            object.put("queryList", array);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        PublicRequest.customMainRequestPost(this, UrlValues.PREPAY_HOURORDER_URL, handler, object);

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1000:
                    break;
                case 2000:
                    if (msg.obj.toString() != null) {
                        System.out.print("prepay_hourorder_url" + msg.obj.toString());
                    }
                    break;
                case 3000:
                    if (msg.obj.toString() != null) {
                        System.out.print("prepay_hourorder_url3000" + msg.obj.toString());
                    }
                    break;
            }

        }
    };

    private void changeStatus(int index) {
        Log.e("Long", "index" + index);
        ivAliPay.setBackgroundResource(R.mipmap.amt_no_select);
        ivWechat.setBackgroundResource(R.mipmap.amt_no_select);
        ivUnionPay.setBackgroundResource(R.mipmap.amt_no_select);
        switch (index) {
            case typeAlipay:
                ivAliPay.setBackgroundResource(R.mipmap.amt_select);
                break;
            case typeWeChat:
                ivWechat.setBackgroundResource(R.mipmap.amt_select);
                break;
            case typeUnionPay:
                ivUnionPay.setBackgroundResource(R.mipmap.amt_select);
                break;
            default:
                break;
        }
    }
}
